//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.Utility;
import tv.dyndns.kishibe.client.packet.PacketProblemData;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;

public class WidgetInputMojiPanel extends WidgetInputBase implements ClickHandler {
	private final ButtonLetter button[] = new ButtonLetter[10];
	private final Grid grid = new Grid(2, 5);
	private final WidgetAnswer widgetAnswer;
	private final int answerLength;

	public WidgetInputMojiPanel(WidgetAnswer widgetAnswer, PacketProblemData problemData) {
		this.widgetAnswer = widgetAnswer;
		this.answerLength = problemData.answer[0].length();

		final String choice = problemData.choice[0];
		final int perm[] = Utility.getInstance().makePermutationArray(10);

		for (int i = 0; i < 10; ++i) {
			button[i] = new ButtonLetter(choice.substring(perm[i], perm[i] + 1), widgetAnswer);
			button[i].setStyleName("gwt-Button-mojipanel");
			grid.setWidget(i / 5, i % 5, button[i]);
			button[i].addClickHandler(this);
		}

		add(grid);
		add(new HTML("上記から" + answerLength + "文字を選んで答えてください<br/>同じ文字を何度でも使えます"));
	}

	public void enable(boolean b) {
		for (int i = 0; i < 10; ++i) {
			button[i].setEnabled(b);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		if (widgetAnswer.getText().length() == answerLength) {
			sendAnswer(widgetAnswer.getText());
		} else {
			playSound(Constant.SOUND_URL_BUTTON_PUSH);
		}
	}
}
